<?php

class Index extends Base_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view('index');
    }

    public function welcome()
    {

        $switchYear = $this->input->get('year') ? $this->input->get('year'): "本周" ;
        $this->load->model('Bill_model', 'bill');
        $today_time     = date('Y-m-d', time());
        $week_time      = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y")));
        $month_time     = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1, date("Y")));
        //采购总价
        $total     = $this->bill->getPurchaseTotalPrice([]);
        $today     = $this->bill->getPurchaseTotalPrice(['timelimit' => $today_time]);
        $week      = $this->bill->getPurchaseTotalPrice(['timelimit' => $week_time]);
        $month     = $this->bill->getPurchaseTotalPrice(['timelimit' => $month_time]);

        $orders_total     = $this->bill->getOrdersCnt([]);
        $orders_today     = $this->bill->getOrdersCnt(['timelimit' => $today_time]);
        $orders_week      = $this->bill->getOrdersCnt(['timelimit' => $week_time]);
        $orders_month     = $this->bill->getOrdersCnt(['timelimit' => $month_time]);
        $yearList     = ['本周', '2018','2017','2016'];
        if ($switchYear == '本周') {
            //本周数据
            $date = new DateTime();
            $date->modify('this week');
            $first_day_of_week = $date->format('Y-m-d');
            $this_week         = [];
            for ($weeklengh = 0; $weeklengh <= 6; $weeklengh++) {
                $this_week[] = $date->modify("this week +{$weeklengh} days")->format('Y-m-d');
            }
            $weekRecord      = $this->bill->getThisWeekData($this_week);
            $hchar['xAxis']  = $weekRecord['days'];
            $hchar['cnt']    = $weekRecord['cnt'];
            $hchar['profit'] = $weekRecord['profit'];
        } else {
            $getEveryMonthRecordByYear = $this->bill->getEveryMonthRecordByYear($switchYear);

            $hchar['xAxis']         = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
            $everyMonthRecordByYear = [];
            foreach ($getEveryMonthRecordByYear as $row) {
                $everyMonthRecordByYear['cnt'][]    = (int)$row['cnt'];
                $everyMonthRecordByYear['profit'][] = (int)$row['profit'];
            }
            $hchar['cnt']    = $everyMonthRecordByYear['cnt'];
            $hchar['profit'] = $everyMonthRecordByYear['profit'];
        }
        $data             = [
            'purchaseTotalPrice' => [
                'total' => (int)$total['puerchase_price'] ?? 0,
                'today' => (int)$today['puerchase_price'] ?? 0,
                'week'  => (int)$week['puerchase_price'] ?? 0,
                'month' => (int)$month['puerchase_price'] ?? 0,
            ],
            'selling_rice'           => [
                'total' => (int)$total['selling_price'] ?? 0,
                'today' => (int)$today['selling_price'] ?? 0,
                'week'  => (int)$week['selling_price'] ?? 0,
                'month' => (int)$month['selling_price'] ?? 0,
            ],
            'profit'                 => [
                'total' => (int)($total['selling_price'] - $total['puerchase_price'])  ?? 0,
                'today' => (int)($today['selling_price'] - $today['puerchase_price'])  ?? 0,
                'week'  => (int)($week['selling_price'] - $week['puerchase_price'])  ?? 0,
                'month' => (int)($month['selling_price'] - $month['puerchase_price'])  ?? 0,
            ],
            'orders'             => [
                'total'     => $orders_total,
                'today'     => $orders_today,
                'week'      => $orders_week,
                'month'     => $orders_month,
            ],
            'hchar'                  => $hchar,
            'switch_year'=>$switchYear,
            'yearList'=>$yearList
        ];
        $this->load->view('welcome', $data);
    }
}
